 //控制层 
app.controller('goodsController' ,function($scope,$controller,$location,itemCatService,goodsService){
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		goodsService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		goodsService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.totals;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){				
		goodsService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	}
	
	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=goodsService.update( $scope.entity ); //修改  
		}else{
			serviceObject=goodsService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
		        	$scope.reloadList();//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}


    $scope.deleteById = function(){
        goodsService.dele($scope.selectIds).success(
            function(response){
            if(response.success){
                $scope.reloadList();
            }else{
                alert(response.message);
            }


        } )     }
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		goodsService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.totals;//更新总记录数
			}			
		);
	}
    $scope.entity = {};
//   展示所有为审核的商品
	$scope.findByStatus = function(id) {
        goodsService.findByStatus(id).success(
        	function (response) {
                $scope.entity.toGoods = response;
        });

    };
//	审核商品
    $scope.check = function (status) {
        goodsService.check(status,$scope.selectIds).success(
            function (response) {
            	if (response.success) {
                    $scope.reloadList();
                }else {
                    alert(response.message);
                }

            }
        );
    };
    $scope.itemCatList = [];
//    查询所有的itemCat
    $scope.itemCatFindAll = function () {
        itemCatService.itemCatFindAll().success(function (response) {
            for (var i = 0; i < response.length; i++) {
                $scope.itemCatList[response[i].id] = response[i].name;
            }
        });
    };
    //数据回显
    $scope.findOne = function () {
        var search = $location.search()['id'];
        if (search == null) {
            return;
        }
        goodsService.findOne(search).success(
            function (response) {
                debugger;
                $scope.entity = response;
                if ($scope.entity.tbGoodsDesc == null) {
                    $scope.entity.tbGoodsDesc = {itemImages: [], specificationItems: [],customAttributeItems:[]};
                }else{
                    $scope.entity.tbGoodsDesc.customAttributeItems = JSON.parse($scope.entity.tbGoodsDesc.customAttributeItems);
                    $scope.entity.tbGoodsDesc.itemImages = JSON.parse($scope.entity.tbGoodsDesc.itemImages);
                    $scope.entity.tbGoodsDesc.specificationItems = JSON.parse($scope.entity.tbGoodsDesc.specificationItems);
                    for (var i = 0; i < $scope.entity.itemList.length; i++) {
                        $scope.entity.itemList[i].spec = JSON.parse($scope.entity.itemList[i].spec);
                    }
                }
                try {
                    editor.html($scope.entity.tbGoodsDesc.introduction);
                } catch (e) {
                    editor.html("");
                }



            }
        );
    }
});	
